package com.ljxt.dt.dao.storemanagementdao.impl;

import com.ljxt.dt.dao.storemanagementdao.AddReceptDao;
import com.ljxt.dt.domain.*;
import com.ljxt.dt.util.DaoUtil;
import com.ljxt.dt.vo.storemanagement.AddFindFoodsVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-12 17:27
 * @copyright 老九学堂
 */

public class AddReceptDaoImpl implements AddReceptDao {
    @Override
    public List<ReceptType> selectAddType() {
        String sql ="select * from recept_type where state > 0 ORDER BY state";

        return DaoUtil.querySelectAll(sql,new ReceptType());
    }

    @Override
    public List<SupplierInfo> selectSupplier() {
        String sql = "select * from supplier_info";
        return DaoUtil.querySelectAll(sql,new SupplierInfo());
    }

    @Override
    public List<GoodsInfo> findFoods(AddFindFoodsVO addFindFoodsVO,int pageIndex,int pageSize) {

        String sql = "select * from goods_info where 1=1 ";

        List<Object> params = new ArrayList<>();
        if(addFindFoodsVO.getSupplierId() !=null){
            sql += "and id  in(select goods_id from supplier_goods where supplier_id = ?) ";
            params.add(addFindFoodsVO.getSupplierId());
        }
        if(addFindFoodsVO.getGoodsId() != null){
            sql+=" and  id = ?";
            params.add(addFindFoodsVO.getGoodsId());
        }
        if(addFindFoodsVO.getGoodsName() != null){
            sql += " and  goods_name like ?";
            String obj =  "%"+addFindFoodsVO.getGoodsName()+"%";
            params.add(obj);
        }

        sql += " limit ?,?";
        pageIndex=(pageIndex-1)*pageSize;
        params.add(pageIndex);
        params.add(pageSize);


        return DaoUtil.querySelectAll(sql,new GoodsInfo(),params.toArray());
    }

    @Override
    public int selectTotal(AddFindFoodsVO addFindFoodsVO) {

        String sql = "select count(*) from goods_info where 1=1 ";

        List<Object> params = new ArrayList<>();
        if(addFindFoodsVO.getSupplierId() !=null){
            sql += "and id  in(select goods_id from supplier_goods where supplier_id = ?) ";
            params.add(addFindFoodsVO.getSupplierId());
        }
        if(addFindFoodsVO.getGoodsId() != null){
            sql+=" and  id = ?";
            params.add(addFindFoodsVO.getGoodsId());
        }
        if(addFindFoodsVO.getGoodsName() != null){
            sql += " and  goods_name like ?";
            String obj =  "%"+addFindFoodsVO.getGoodsName()+"%";
            params.add(obj);
        }



        return DaoUtil.querySelectCount(sql,params.toArray());
    }

    @Override
    public List<GoodsInfo> selectGoodsInfo(String[] strs) {

        //循环遍历数组得到参数并生成sql语句
        String sql = " select * from goods_info where id in(";
        List<Object>  params = new ArrayList<>();
        //如果strs是null则不加?
        if(strs == null){

        }else{

            for (int i = 0; i < strs.length; i++) {
                params.add(strs[i]);
                sql+="?,";
            }
        }
        //这里判断是否有参数 没有则就让它查不到
        if(params.size() !=0){
            sql = sql.substring(0,sql.length()-1);
            sql+=")";
        }else {
            sql = "select * from goods_info where id in(-1)";
        }





        return DaoUtil.querySelectAll(sql,new GoodsInfo(),params.toArray());
    }

    @Override
    public List<String> selectGoodsType(Integer id) {
        String sql = "select id from   category_info where  goods_id = ?";
        return DaoUtil.querySelectStringAll(sql,id);
    }

    @Override
    public CategoryInfo selectGoodsInfoName(String str) {
        String sql = "select * from   category_info where  id = ?";
        return DaoUtil.querySelectOnly(sql,new CategoryInfo(),str);
    }

    @Override
    public List<AttrInfo> selectGoodsInfoType(String str) {
        String sql =  "select * from attr_info where category_id = ?";

        return DaoUtil.querySelectAll(sql, new AttrInfo(),str);
    }

    @Override
    public int insertRepect(ReceptInfo receptInfo) {
        List<Object> map = DaoUtil.getSqlAndValue(receptInfo);
        String sql = (String) map.get(0);
        List<Object> params = (List<Object>) map.get(1);

        return DaoUtil.updateDate(sql,params.toArray());
    }

    @Override
    public int insertRepectDetail(ReceptDetail receptDetail) {
        List<Object> map = DaoUtil.getSqlAndValue(receptDetail);
        String sql = (String) map.get(0);
        List<Object> params = (List<Object>) map.get(1);

        return DaoUtil.updateDate(sql,params.toArray());
    }
}
